Page({
    data: {
        books: [],
        bookCoverUrl : 'https://ov-library-1316830564.cos.ap-guangzhou.myqcloud.com/bookCover%2F',
        defaultCover : '/resource/images/defaultCover.png'
    },

    onLoad() {
        let favoriteIds = [];
        favoriteIds = wx.getStorageSync('favorites') || [];
        favoriteIds = favoriteIds.map(item => item.replace(/"/g, '')).join(', ')
        console.log(favoriteIds)

        if (!favoriteIds.length) {
            wx.showToast({
                title: '暂无收藏',
                icon: 'none'
            });
            return;
        }

        wx.request({
            url: 'http://localhost:8080/books/queryByIds',
            method: 'GET',
            data: {
                bookIds: favoriteIds
            },
            success: res => {
                if (res.data.code === 0) {
                    this.setData({
                        books: res.data.data
                    });
                } else {
                    wx.showToast({
                        title: '获取数据失败',
                        icon: 'none'
                    });
                }
            },
            fail: () => {
                wx.showToast({
                    title: '请求失败',
                    icon: 'none'
                });
            }
        });
    },

    goToBookDetail(event) {
        console.log("event:"+event)
        const bookId = event.currentTarget.dataset.id;
        wx.navigateTo({
          url: `/pages/books/detailed/detailed?id=${bookId}`
        });
      },

});